﻿﻿package com.glidecast.models
{
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.net.URLVariables;
	
	import flash.display.*;

	import flash.events.Event;
	
	import com.glidecast.common.gcSettings;
	
	public class Graphic extends EventDispatcher
	{

		public static var UPDATE:String='GraphicUpdated';
		public static var COMPLETE:String='GraphicDownloadComplete';		
		
		public var staticGraphic:DisplayObject;
		public var dynamicGraphic:Loader;
		
		private var ldr:Loader = new Loader();	
		public function Graphic()
		{	
		}
		
		private var _animation:String='';
		public function get animation():String
		{
			return _animation;
		}
		public function set animation(s:String):void
		{
			_animation=s;
			dispatchEvent(new Event(Graphic.UPDATE));			
		}
		
		private var _effect:String='';
		public function get effect():String
		{
			return _effect;
		}
		public function set effect(s:String):void
		{
			_effect=s;
			dispatchEvent(new Event(Graphic.UPDATE));			
		}
		
		private var _url:String='';
		public function get url():String
		{
			return _url;
		}
		public function set url(s:String):void
		{
			_url=s;			
		}
		private var _type:String='';
		public function get type():String
		{
			return _type;
		}
		public function set type(s:String):void
		{
			_type=s;			
		}
		public function load():void
		{	
			var url:String = this.url;
			var urlReq:URLRequest = new URLRequest(url);
			ldr.contentLoaderInfo.addEventListener("complete", imageLoaded);
			ldr.load(urlReq);
		}
		public function imageLoaded(evtObj:Event):void 
		{
			//use this function to determine if this is
			//stored as a static image or SWF.
			
			if (evtObj.target.contentType != null)
			{
				switch (evtObj.target.contentType)
				{
					case "image/jpeg":
					case "image/gif":
					case "image/png":
					{
						//it is a static image, set the type.
						this.type = "gif";
						var image:Bitmap= evtObj.target.content as Bitmap;
						image.smoothing = true;			
						image.pixelSnapping = "never";
						image.x = 0;
						image.y = 0;
						image.width = gcSettings.PLAYER_WIDTH;
						image.height = gcSettings.PLAYER_HEIGHT;						
						this.staticGraphic = image;

						break;
					}
					case "application/x-shockwave-flash":
					{						
						this.type="swf";
						var movie:Loader = evtObj.currentTarget.loader as Loader;
						movie.x = 0;
						movie.y = 0;
						movie.width = gcSettings.PLAYER_WIDTH;
						movie.height = gcSettings.PLAYER_HEIGHT;						
						this.staticGraphic = movie;						
						break;
					}
				}
			}			
			//broadcast this event.
			dispatchEvent(new Event(Graphic.COMPLETE));
			
		}
	}
}
